#include "FITKAcousticsFDiscNodeListAdaptor.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"
#include "FITKAcousticsHDF5Writer.h"
#include "FITKAcousticsHDF5Reader.h"
bool IO::FITKAcousticsFDiscNodeListAdaptor::adaptR()
{
    Acoustics::FITKAcousticsDiscNodeList* NodeList =
        dynamic_cast<Acoustics::FITKAcousticsDiscNodeList*>(_dataObj);
    if (NodeList == nullptr || _reader == nullptr) return false;
    m_toolp = _reader->getHDF5FileTool();//初始化工具

    std::tuple<bool, H5::Group>Ret = m_toolp->openGp("NodeList");//打开节点列表组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group gp = std::get<1>(Ret);
    return this->readNodeList(gp, NodeList);
}

bool IO::FITKAcousticsFDiscNodeListAdaptor::adaptW()
{
    Acoustics::FITKAcousticsDiscNodeList* NodeList =
        dynamic_cast<Acoustics::FITKAcousticsDiscNodeList*>(_dataObj);
    if (NodeList == nullptr || _writer == nullptr) return false;
    m_toolp = _writer->getHDF5FileTool();//初始化工具


    std::tuple<bool, H5::Group> Ret = m_toolp->createGp("NodeList");     //创建节点列表组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group gp = std::get<1>(Ret);


    return this->writeNodeList(gp, NodeList);
}

bool IO::FITKAcousticsFDiscNodeListAdaptor::readNodeList(H5::Group & gp, Acoustics::FITKAcousticsDiscNodeList * NodeListP)
{
 
    if (NodeListP == nullptr)
    {
        return false;
    }

    std::vector<std::vector<double>>data;

    if(m_toolp->ReadTwoArrayDataSetDouble(gp, "NodeListData", data)==false)
    {
        return false;
    }

    //节点数据回写
    for (unsigned int i = 0; i < data.size(); ++i)
    {
        int id = static_cast<int>(data[i].at(3));
        double x = data[i].at(0);
        double y = data[i].at(1);
        double z = data[i].at(2);
        NodeListP->addNode(id, x, y, z);
    }
    return true;
}

bool IO::FITKAcousticsFDiscNodeListAdaptor::writeNodeList(H5::Group & gp, Acoustics::FITKAcousticsDiscNodeList * data)
{
    if (data == nullptr)
    {
        return false;
    }
    int NodeCount = data->getNodeCount();
    std::vector<std::vector<double>>Node;
    for (int i = 0; i < NodeCount; ++i)//构建二维数组
    {
        Core::FITKNode* node = data->getNodeAt(i);
        if (node == nullptr)
        {
            return false;
        }
        double Coor[3] = { 0.0,0.0,0.0 };
        node->getCoor(Coor);
        int ID=node->getNodeID();
        std::vector<double> temp;
        temp.push_back(Coor[0]);
        temp.push_back(Coor[1]);
        temp.push_back(Coor[2]);
        temp.push_back(static_cast<double>(ID));
        Node.push_back(temp);
    }
    if (m_toolp->CreateTwoArrayDataSetDouble(gp, "NodeListData", Node)==false)//写二维数组
    {
        return false;
    }
    return true;
}
